import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
import { dailyViewsIpFunc } from '@/utils/kanban.js'


Vue.use(VueRouter)
// 避免重复点击
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  let query = location.query
  let path = location.path
  let str = ''
  for(let m in query){
    str+= '&'+m+'='+query[m]
  }
  str = str.substring(1)
  if(str){
    path += '?'+str
  }
  
  if(path == this.currentRoute.fullPath) {
    window.scrollTo(0,0)
    window.location.reload()
  }
  return originalPush.call(this, location)
}
/* Layout */
import Layout from '../views/layout/index'
export const constantRouterMap = [
  {
    path: '/', 
    component: Layout,  
    redirect: '/index',
    meta: {title: '系统配置', hideen: true},
    children:[
     
      {path: '/signUp', component:()=>import('@/views/register/register.vue'), },
      {path: '/register', component: ()=>import('../views/register/warranty/warrantySignUp.vue'), },
      {path: '/login', component: ()=>import('@/views/register/login.vue'), },
      {path: '/warranty', component: ()=>import('../views/support/warrantyform.vue'), },
      {path: '/warranty_success', component: ()=>import('@/views/support/warranty_success.vue'), },

      {path: '/betatester', component: ()=>import('@/views/register/betaTester/betaTester.vue'), },
      {path: '/betatester_guide', component: ()=>import('@/views/register/betaTester/betaTesterGuide.vue'), },
      {path: '/betatester_active', component: ()=>import('@/views/register/betaTester/betatester_active.vue'), },
      {path: '/betatester_success', component: ()=>import('@/views/register/betaTester/betatester_success.vue'), },
      {path: '/betatester_active_success', component: ()=>import('@/views/register/betaTester/betatester_active_success.vue'), },
      {path: '/betatester_active_guide', component: ()=>import('@/views/register/betaTester/betatester_active_guide.vue'), },
      {path: '/search', component: ()=>import('@/views/other/search/index.vue'), },
      {path: '/bonochtester', component: ()=>import('@/views/register/bonochtester/index.vue'), },
      {path: '/bonochtester_success', component: ()=>import('@/views/register/bonochtester/bonochtester_success.vue'), },
      {path: '/register_betatester_guide', component: ()=>import('@/views/support/register_betatester_guide.vue'), },

      {path: '/register_email', component: ()=>import('../views/register/registerEmail.vue'), },
      {path: '/register_signin', component: ()=>import('../views/register/registerSignin.vue'), },
      {path: '/register_email_vaild', component: ()=>import('../views/register/register_email_vaild.vue'), },

      {path: '/forget_password', component: ()=>import('../views/register/password/forget_password.vue'), },
      {path: '/reset_password', component: ()=>import('../views/register/password/reset_password.vue'), },
      {path: '/reset_password_success', component: ()=>import('../views/register/password/reset_password_success.vue'), },
      {path: '/forget_passoword_email', component: ()=>import('../views/register/password/forget_passoword_email.vue'), },
      {path: '/reset_password_not_vaild', component: ()=>import('../views/register/password/reset_password_not_vaild.vue'), },
  
      // 个人中心
      { path: '/personal_center', component: () => import('@/views/peosonalCenter/index.vue'),   meta: {title: '个人中心', hideen: false},
        children: [
          {path: '/my_benefits', component: ()=>import('../views/peosonalCenter/myBenefits/index.vue'), },
          {path: '/my_devices', component: ()=>import('../views/peosonalCenter/myDevices/index.vue'), },
          {path: '/userinfo', component: ()=>import('../views/peosonalCenter/userInfo/index.vue'), },
        ]
      },
      // 底部 about 
      { path: '/about', component: () => import('@/views/about/index.vue'),   meta: {title: 'about', hideen: false},
        children: [
          {path: '/ourstory', component: ()=>import('../views/about/rampow/index.vue'), },
          {path: '/contact_info', component: ()=>import('../views/about/contact/index.vue'), },
          {path: '/warrantyPolicy', component: ()=>import('../views/about/warrantyPolicy/index.vue'), },
          {path: '/termsOfService', component: ()=>import('../views/about/termsOfService/index.vue'), },
          {path: '/privacyPolicy', component: ()=>import('../views/about/privacyPolicy/index.vue'), },
          {path: '/ourpartner', component: ()=>import('@/views/about/ourpartner'),   meta: {title: '', hideen: false}, },

        ]
      },
      // { path: '/products', component: ()=>import('@/views/products/productList.vue'),   meta: {title: '', hideen: false},
      //   children: [
      //     {path: '/products/:id', component: ()=>import('@/views/products/productList.vue'),   meta: {title: '', hideen: false},}
      //   ] 
      // },
      {path: '/contact_us', component: ()=>import('@/views/support/contactUs.vue'),   meta: {title: '',  hideen: false}, },
      { path: '/FAQ', component: () => import('@/views/support/FAQ/index.vue'),   meta: {title: 'about', hideen: false},
       children: [
         {path: '/FAQ/:faqtype/:faqSecondType', component: ()=>import('../views/support/FAQ/faqTemplate/index.vue'), },
         {path: '/FAQ/:faqtype/', component: ()=>import('../views/support/FAQ/faqTemplate/index.vue'), },

       ]
      },
      {path: '/download', component: ()=>import('@/views/support/download/index.vue'), },
      
    ],
  },
  {path: '/index', component: ()=>import('@/views/index/index'),   meta: {title: 'Shop All'}, },
  // 产品详情
  {path: '/product/:id', component: () => import('@/views/products/detail.vue'), meta: {title: '产品详情', hideen: false}, },
  { path: '/article/:id', component: Layout,   meta: {title: '文章详情', hideen: false},
    children: [
      {path: '/article/:id', component: () => import('@/views/community/details.vue'), meta: {title: '产品详情', hideen: false}, },
    ],
  },  
]

export const asyncRouterMap = [
  // 产品列表
  { path: '/products', component: ()=>import('@/views/products/productList.vue'),   meta: {title: '', hideen: false},
    children: [
      {path: '/products/:id', component: ()=>import('@/views/products/productList.vue'),   meta: {title: '', hideen: false},},
    ] 
  },

  {
    path: '/beta_tester', component: Layout,   meta: {title: '系统配置', hideen: false},
    children:[
      {path: '/betatester', component: ()=>import('@/views/index/index'),   meta: {title: '', hideen: false}, },
      {path: '/beta_tester_faq', component: ()=>import('@/views/index/index'),   meta: {title: '',  hideen: false}, },
    ]
  },
  {
    path: '/community', component: Layout,   meta: {title: '文章列表'}, 
    children:[
      {path: '/community/:id', component: ()=>import('@/views/community/list'),   meta: {title: '', hideen: false}, },
    ]
  },

]
const router = new VueRouter({
  mode: 'history', //后端支持可开
  scrollBehavior: () => ({y: 0}),
  routes: constantRouterMap.concat(asyncRouterMap)
})
 
const array = ['/signUp', '/login', '/forget_password', '/reset_password',]
router.beforeEach((to, from, next) => {
  let isRegister = array.indexOf(to.path) > -1
  store.dispatch('ToggleRegister',isRegister)
  dailyViewsIpFunc(to.path)
  // console.log(from, to.path)
  
  next()
})

export default router
